<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require APPPATH . 'controllers/kint/Kint.class.php';
class Tasks extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->is_logged_in();
	}

	public function index()
	{
		$this->load->model('users_model');
		$data['username'] = $this->get_username();
		$data['main_content'] = 'tasks';
		$data['user_id'] = $this->get_userid();
		$this->init($data);
	}

	function init($data){
		$data['tasks'] = $this->tasks_model->get_all();
		$this->load->view('includes/template.php',$data);
	}

	function view_task(){
		$this->load->model('users_model');
		$this->load->model('comments');
		$url = $this->uri->segment(2);
		$data['username'] = $this->get_username();
		$data['task'] = $this->tasks_model->get_single_task($url);
		$data['comments'] = $this->comments->get_comments($data['task']->id);
		$data['main_content'] = 'view_task';
		$data['user_id'] = $this->get_userid();
		$this->load->view('includes/template.php',$data);
	}


	function add_new_task($valid = false){
		$data['username'] = $this->get_username();
		$data['valid'] = $valid;
		$this->load->model('users_model');
		$data['users'] = $this->users_model->get_all_users();
		$data['user_id'] = $this->get_userid();
		$data['main_content'] = 'create_task.php';
		$this->load->view('includes/template.php',$data);
	}

	function delete(){
		$this->load->model('users_model');
		$url = $this->uri->segment(3);
		$this->tasks_model->delete($url);
		redirect('/manage/manage_tasks');
	}


	function get_user_by_id($id){
		$this->load->model('users_model');
		return $this->users_model->get_user_by_id($id);
	}

	function is_logged_in(){
		$is_logged_in = $this->session->userdata('is_logged_in');
		if (!isset($is_logged_in) || $is_logged_in != true) {
			redirect('/');
			die();
		}
	}

	function logout(){
		redirect('/login');
		$this->session->sess_destroy();
	}

	function get_username(){
		$session = $this->session->all_userdata();
		return $session['username'];
	}

	function get_userid(){
		$session = $this->session->all_userdata();
		return $session['id'];
	}


	function edit(){
		$this->load->model('users_model');
		$url = $this->uri->segment(3);
		$data['username'] = $this->get_username();
		$data['users'] = $this->users_model->get_all_users();
		$data['task'] = $this->tasks_model->get_single_to_edit($url);
		$data['main_content'] = 'tasks/edit_task.php';
		$this->load->view('includes/template.php',$data);
	}


	function update_task(){

		$url = $this->input->post('edit_url');
		date_default_timezone_set('GMT');
		$task = array(
			'id'=> $_POST['edit_id'],
			'title'=> $_POST['edit_title'],
			'content' => $_POST['edit_content'],
			'date_due' => date('Y-m-d', strtotime($this->input->post('edit_date_due'))),
			'responsible' =>json_encode($_POST['edit_responsible']),
			'accountable' =>json_encode($_POST['edit_accountable']),
			'consulted' =>json_encode($_POST['edit_consulted']),
			'informed' =>json_encode($_POST['edit_informed']),
			'priority' => $_POST['edit_priority'],
			'status' => $_POST['edit_status']
		);

		$query = $this->tasks_model->update($task);
		if ($query) {
			redirect('/tasks/'.$url);
		}
	}


}

/* End of file home.php */
/* Location: ./application/controllers/home.php */

















